package com.kyivmohyla.kma2.client.utils;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.googlecode.gwtTableToExcel.client.TableToExcelClient;
import com.kyivmohyla.kma2.shared.model.UIEquipment;
import com.kyivmohyla.kma2.shared.model.UIService;

import java.util.List;

public class OrdersExport {

    private static OrdersExport instance = new OrdersExport();

    public static OrdersExport getInstance() {
        if (instance == null) {
            instance = new OrdersExport();
        }
        return instance;
    }

    public FormPanel go(List<ExportOrder> exportOrders) {
        TableToExcelClient tableToExcelClient = new TableToExcelClient(getOrderTable(exportOrders), new Button("Submit"));
        return tableToExcelClient.getExportWidget();
    }

    private Grid getOrderTable(List<ExportOrder> exportOrders) {
        Grid grid = new Grid(1, 3);
        grid.setTitle("Exported orders");
        int lastRow = 0;
        for (ExportOrder order : exportOrders) {
            lastRow = addOrderInfoToGrid(grid, order, lastRow);
        }
        return grid;
    }

    private int addOrderInfoToGrid(Grid grid, ExportOrder order, int row) {
        grid.insertRow(row);
        grid.setText(row++, 0, "Order export");
        grid.insertRow(row);
        grid.setText(row, 0, "Id");
        grid.setText(row++, 1, String.valueOf(order.getOrderId()));
        grid.insertRow(row);
        grid.setText(row, 0, "State");
        grid.setText(row++, 1, order.getStateName());
        grid.insertRow(row);
        grid.setText(row, 0, "Create Date");
        grid.setText(row++, 1, order.getCreateDate().toString());
        grid.insertRow(row);
        grid.setText(row, 0, "Start Date");
        grid.setText(row++, 1, order.getStartDate().toString());
        grid.insertRow(row);
        grid.setText(row, 0, "End Date");
        grid.setText(row++, 1, order.getEndDate().toString());
        grid.insertRow(row);
        grid.setText(row, 0, "Commentary");
        grid.setText(row++, 1, order.getCommentary());
        grid.insertRow(row++);
        grid.insertRow(row);
        grid.setText(row++, 0, "Customer");
        grid.insertRow(row);
        grid.setText(row, 0, "Customer Name");
        grid.setText(row++, 1, order.getCustomerName());
        grid.insertRow(row);
        grid.setText(row, 0, "Customer Phone");
        grid.setText(row++, 1, order.getCustomerPhone());
        grid.insertRow(row);
        grid.setText(row, 0, "Customer Address");
        grid.setText(row++, 1, order.getCustomerAddress());
        grid.insertRow(row);
        grid.setText(row, 0, "Customer Email");
        grid.setText(row++, 1, order.getCustomerEmail());
        if (order.getServices() != null && order.getServices().size() > 0) {
            grid.insertRow(row++);
            grid.insertRow(row);
            grid.setText(row++, 0, "Services");
            grid.insertRow(row);
            for (UIService service : order.getServices()) {
                grid.insertRow(row);
                grid.setText(row, 0, String.valueOf(service.getServiceId()));
                grid.setText(row++, 1, service.getName());
            }
        }
        if (order.getEquipments() != null && order.getEquipments().size() > 0) {
            grid.insertRow(row++);
            grid.insertRow(row);
            grid.setText(row++, 0, "Equipment");
            grid.insertRow(row);
            for (UIEquipment equipment : order.getEquipments()) {
                grid.insertRow(row);
                grid.setText(row, 0, String.valueOf(equipment.getEquipmentId()));
                grid.setText(row++, 1, equipment.getName());
            }
        }
        grid.insertRow(row++);
        grid.insertRow(row++);
        return row;
    }
}
